﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace DAL
{
    public class ShoppingListDB
    {

        public ShoppingListDB()
        {

        }

        public int CreateShoppingList(int userId, string shoppingListName, int creationDate, int completionDate)
        {
            using (var db = new BalderDataContext())
            {
                ShoppingList shoppingList = new ShoppingList();

                shoppingList.ShoppingListName = shoppingListName;
                shoppingList.CreationDate = creationDate;
                shoppingList.CompletionDate = completionDate;
                shoppingList.Active = true;

                db.ShoppingLists.InsertOnSubmit(shoppingList);
                db.SubmitChanges();

                return shoppingList.ShoppingListId;
            }
        }

        public void RemoveShoppingList(int shoppingListId)
        {
            using (var db = new BalderDataContext())
            {
                var selectRow = db.ShoppingLists.Single(x => x.ShoppingListId == shoppingListId);
                selectRow.Active = false;
                db.SubmitChanges();
                //This is meant for if you want to delete a shoppinglist but since we always want to keep everything in our database
                //so that there isnt any holes in the id numbers
                //var shoppingList = new ShoppingList();

                //shoppingList.ShoppingListId = shoppingListId;

                //foreach (ShoppingListsUser user in shoppingList.ShoppingListsUsers)
                //{
                //    db.GetTable<ShoppingListsUser>().DeleteOnSubmit(user);
                //}

                //foreach (ShoppingListsProduct product in shoppingList.ShoppingListsProducts)
                //{
                //    db.GetTable<ShoppingListsProduct>().DeleteOnSubmit(product);
                //}

                //db.ShoppingLists.DeleteOnSubmit(shoppingList);
                //db.SubmitChanges();
            }
        }

        public void AddUserToShoppingList(int shoppingListId, int userId)
        {
            Debug.WriteLine("Shoppinglist id = " + shoppingListId.ToString());
            using (var db = new BalderDataContext())
            {
                ShoppingListsUser shoppingListsUser = new ShoppingListsUser();

                shoppingListsUser.ShoppingListId = shoppingListId;
                shoppingListsUser.UserId = userId;

                db.ShoppingListsUsers.InsertOnSubmit(shoppingListsUser);
                db.SubmitChanges();
            }
        }

        public void DeleteUserFromShoppingList(int shoppingListId, int userId)
        {
            using (var db = new BalderDataContext())
            {
                var shoppingListsUser =
                    (from u in db.ShoppingListsUsers
                    where u.ShoppingListId == shoppingListId && u.UserId == userId
                    select u).First();

                ShoppingListsUser shoppingListsUser2 = shoppingListsUser;

                db.ShoppingListsUsers.DeleteOnSubmit(shoppingListsUser2);
                db.SubmitChanges();
            }
        }

        public void AddProductToShoppingList(int shoppingListId, int productId, int amount)
        {
            Debug.WriteLine("Shoppinglist id = " + shoppingListId.ToString());
            Debug.WriteLine("Product id = " + shoppingListId.ToString());
            using (var db = new BalderDataContext())
            {
                ShoppingListsProduct shoppingListProduct = new ShoppingListsProduct();

                shoppingListProduct.ShoppingListId = shoppingListId;
                shoppingListProduct.ProductId = productId;
                shoppingListProduct.Amount = amount;
                shoppingListProduct.Bought = false;

                db.ShoppingListsProducts.InsertOnSubmit(shoppingListProduct);
                db.SubmitChanges();
            }
        }

        public void DeleteProductFromShoppingList(int shoppingListId, int productId)
        {
            Debug.WriteLine("Shoppinglist id = " + shoppingListId.ToString());
            Debug.WriteLine("Product id = " + productId.ToString());
            using (var db = new BalderDataContext())
            {
                var shoppingListProduct = new ShoppingListsProduct();
                shoppingListProduct.ShoppingListId = shoppingListId;
                shoppingListProduct.ProductId = productId;

                db.ShoppingListsProducts.Attach(shoppingListProduct);
                db.ShoppingListsProducts.DeleteOnSubmit(shoppingListProduct);
                db.SubmitChanges();
            }
        }

        public void MarkAsBought(int shoppingListId, int productId)
        {

            using (var db = new BalderDataContext())
            {
                var slist = db.ShoppingLists.First(x => x.ShoppingListId == shoppingListId);
                var p = db.Products.Single(x => x.ProductId == productId);
                var sprod = db.ShoppingListsProducts
                              .Where(q => q.ShoppingList == slist)
                              .Where(q => q.Product == p);
                
                ModelLayer.Product pri = new ModelLayer.Product();

            

                foreach (var shoppingListsProduct in sprod)
                {
                    if (shoppingListsProduct.Bought == false)
                    {
                        shoppingListsProduct.Bought = true;
                    }
                    else
                    shoppingListsProduct.Bought = false;
                }

                db.SubmitChanges();
            }
        }

        public List<ModelLayer.ShoppingList> GetShoppingLists(string uname)
        {
            List<ModelLayer.ShoppingList> lister = new List<ModelLayer.ShoppingList>();
            using (var db = new BalderDataContext())
            {
                var us = db.Users.Single(x => x.UserName == uname);

                var liste = db.ShoppingListsUsers
                                .Where(o => o.User == us)
                                .Select(o => o.ShoppingList)
                                .Where(x => x.Active==true)
                                .Distinct();

                
                
                foreach (var shoppingList in liste)
                {
                    ModelLayer.ShoppingList slist = new ModelLayer.ShoppingList();
                    Debug.WriteLine("db Lag: " +shoppingList.ShoppingListName);
                    slist.ShoppingListId = shoppingList.ShoppingListId;
                    slist.ShoppingListName = shoppingList.ShoppingListName;
                    slist.CompletionDate = (int)shoppingList.CompletionDate;
                    slist.CreationDate = shoppingList.CreationDate;
                    lister.Add(slist);
                }
            }
            return lister;
        }

        public List<ModelLayer.Product> GetProductsFromShoppingList(int slistId)
        {
            List<ModelLayer.Product> products = new List<ModelLayer.Product>();
             using (var db = new BalderDataContext())
             {
                 var slist = db.ShoppingLists.Single(x => x.ShoppingListId == slistId);

                 var prodListe = db.ShoppingListsProducts
                                   .Where(o => o.ShoppingList == slist)
                                   .Select(o => o.Product)
                                   .Distinct();

                 foreach (var product in prodListe)
                 {
                     ModelLayer.Product p = new ModelLayer.Product();
                     p.ProductId = product.ProductId;
                     p.ProductName = product.ProductName;
                     p.Amount = db.ShoppingListsProducts.First(a => a.ProductId == product.ProductId).Amount;
                     p.Bought = db.ShoppingListsProducts.First(a => a.ProductId == product.ProductId).Bought;
                     products.Add(p);
                 }
             }
            return products;
        }
        public List<ModelLayer.User> GetSharedUsersForShoppingList(string uname, int slist)
        {
            List<ModelLayer.User> u = new List<ModelLayer.User>();
            using (var db = new BalderDataContext())
            {
                var user = db.Users.Single(x => x.UserName == uname);
                var users = db.ShoppingListsUsers
                              .Where(o => o.ShoppingListId == slist)
                              .Select(o => o.User)
                              .Distinct();

                foreach (var user1 in users)
                {
                    ModelLayer.User us = new ModelLayer.User();
                    us.FName = user1.FName;
                    us.LName = user1.LName;
                    us.UserId = user1.UserId;
                    us.UserName = user1.UserName;
                    us.Email = user1.Email;
                    u.Add(us);
                }
            }
            return u;
        }
    }
}
